﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArgusLib.Fractals;
using ArgusLib.Fractals.OrbitFractals;

namespace Fraktalysator.Controls
{
	partial class RendererBuddhabrotInputControl : UserControl, IRenderParameterInputControl
	{
		bool requestRedraw = true;

		public RendererBuddhabrotInputControl()
		{
			InitializeComponent();
			this.cbColoring.Initialize(typeof(RenderParameterBuddhabrot.Colorings), Localization.Enums.ResourceManager);
			this.bAddColor.BackgroundImage = Resources.Icons16.Add;
		}

		public RenderParameterBuddhabrot Parameter
		{
			get
			{
				RenderParameterBuddhabrot para = new RenderParameterBuddhabrot();
				para.BackgroundColor = this.cbBackgroundColor.SelectedColor;
				para.Coloring = (RenderParameterBuddhabrot.Colorings)this.cbColoring.SelectedEnum;
				para.RangeMultiplier = (float)this.nudRangeMultiplier.Value;
				para.OffsetMultiplier = (float)this.nudOffsetMultiplier.Value;
				para.UserColors = new Color[this.pUserColors.Controls.Count];
				for (int i = 0; i < this.pUserColors.Controls.Count; i++)
					para.UserColors[i] = ((ColorListItem)this.pUserColors.Controls[i]).ColorComboBox.SelectedColor;
				return para;
			}
			set
			{
				this.requestRedraw = false;
				this.cbBackgroundColor.SelectColor(value.BackgroundColor);
				this.cbColoring.Select(value.Coloring.ToUInt64());
				this.nudRangeMultiplier.Value = new decimal(value.RangeMultiplier);
				this.nudOffsetMultiplier.Value = new decimal(value.OffsetMultiplier);
				foreach (ColorListItem c in this.pUserColors.Controls)
				{
					c.RemoveRequested -= this.listItem_RemoveRequested;
					c.ColorComboBox.SelectedIndexChanged -= this.OnRedrawRequested;
				}
				this.pUserColors.Controls.Clear();
				for (int i = 0; i < value.UserColors.Length; i++)
				{
					ColorListItem listItem = new ColorListItem();
					listItem.ColorComboBox.SelectColor(value.UserColors[i]);
					listItem.RemoveRequested += listItem_RemoveRequested;
					listItem.TabIndex = i;
					listItem.ColorComboBox.SelectedIndexChanged += this.OnRedrawRequested;
					this.pUserColors.Controls.Add(listItem);
				}
				this.requestRedraw = true;
			}
		}

		void listItem_RemoveRequested(object sender, EventArgs e)
		{
			ColorListItem item = (ColorListItem)sender;
			item.RemoveRequested -= this.listItem_RemoveRequested;
			item.ColorComboBox.SelectedIndexChanged -= this.OnRedrawRequested;
			this.pUserColors.Controls.Remove(item);
			this.OnRedrawRequested(this, e);
		}

		IRenderParameter IRenderParameterInputControl.Parameter { get { return this.Parameter; } set { this.Parameter = (RenderParameterBuddhabrot)value; } }

		public Control Control { get { return this; } }

		public event EventHandler RedrawRequested;

		private void OnRedrawRequested(object sender, EventArgs e)
		{
			if (this.requestRedraw == false)
				return;
			if (this.RedrawRequested != null)
				this.RedrawRequested(this, e);
		}

		private void cbColoring_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool visible;
			if (this.cbColoring.SelectedEnum == RenderParameterBuddhabrot.Colorings.UserDefined.ToUInt64())
				visible = true;
			else
				visible = false;

			this.lColors.Visible = visible;
			this.bAddColor.Visible = visible;
			this.pUserColors.Visible = visible;

			if (ArgusLib.Environment.Runtime == ArgusLib.Runtime.Mono)
				this.pUserColors.PerformLayout();

			this.OnRedrawRequested(this, e);
		}

		private void bAddColor_Click(object sender, EventArgs e)
		{
			ColorListItem item = new ColorListItem();
			item.TabIndex = this.pUserColors.Controls.Count;
			item.RemoveRequested += this.listItem_RemoveRequested;
			item.ColorComboBox.SelectColor(Color.Transparent);
			item.ColorComboBox.SelectedIndexChanged += this.OnRedrawRequested;
			this.pUserColors.Controls.Add(item);
			this.OnRedrawRequested(this, e);
		}
	}
}
